// MYSQL 的相关操作函数
#include <iostream>
#include <mysql/mysql.h>

const std::string host = "127.0.0.1";
const std::string user = "root";
const std::string passwd = "Alin@123456";
const std::string db = "regis_datas";
const std::string my_table = "regis_table";
const unsigned int port = 3306;

class Mysql
{
public:
    Mysql()
    {
        initMysql();
    }
    ~Mysql()
    {
        mysql_close(msl);
    }
public:
    int initMysql()
    {
        // 1.初始化
        msl = mysql_init(nullptr);
        if(msl == nullptr){
            std::cerr << "init mysql error..." << std::endl;
            return 1;
        }
        // 2.建立链接
        if(mysql_real_connect(msl, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0) == nullptr)
        {
            std::cerr << "connect mysql error..." << std::endl;
            return 2;
        }
        std::cerr << "connect mysql success!!!";
        // 3. 解决乱码问题
        mysql_set_character_set(msl, "utf-8");
        return 0;
    }
    // 给数据库发送相关命令
    void sendMysqlData(const std::string& mysql_command)
    {
        mysql_query(msl, mysql_command.c_str());
    }
    // 查询数据库相关信息
    std::string searchMysqlData(const std::string& mysql_command)
    {
        mysql_query(msl, mysql_command.c_str());
        MYSQL_RES* select_res = mysql_store_result(msl);
        if(select_res == nullptr){
            std::cerr << "mysql_store_result select error...." << std::endl;
            return "ERROR";
        }
        // 获取表中内部的属性
        my_ulonglong rows = mysql_num_rows(select_res);     // 表的行
        my_ulonglong fields = mysql_num_fields(select_res); // 表的列
        // MYSQL_FIELD* fields_array = mysql_fetch_field(select_res);
        for(int i = 0; i < rows; i++)
        {
            MYSQL_ROW row = mysql_fetch_row(select_res);
            for(int j = 0;j < fields; j++)
            {
                std::cout << row[j] << "\t";
            }
        }
        std::cout << std::endl;
        // 释放结果集
        mysql_free_result(select_res);
    }
    // 查询账号和密码是否存在
    bool isUserExist(const std::string& mysql_command, std::string& my_passwd)
    {
        // select passwd from regis_table where username="张三" and passwd="123456";
        std::cerr << "mysql_command: " << mysql_command << std::endl;
        std::cerr << "my_passwd: " << my_passwd << std::endl;
        mysql_query(msl, mysql_command.c_str());
        MYSQL_RES* select_res = mysql_store_result(msl);
        if(select_res == nullptr){
            std::cerr << "mysql_store_result select error...." << std::endl;
            return false;
        }
        std::cerr << "mysql_store_result select success...." << std::endl;
        // 获取表中内部的属性
        my_ulonglong rows = mysql_num_rows(select_res);     // 表的行
        my_ulonglong fields = mysql_num_fields(select_res); // 表的列
        MYSQL_ROW row = mysql_fetch_row(select_res);
        if(row[0] != my_passwd)
            return false;
        else
            return true;
        // if(row[0] == nullptr) return ;
        // std::string result_passwd = row[0];
        // std::cerr << "result_passwd: " << result_passwd << std::endl;
        
        // return row[0]==my_passwd;
    }
private:
    MYSQL* msl;
};

